# -*- coding: utf-8 -*-
'''
Created on 2011-4-12

@author: Tony
'''
from goplay.activity.models import Activity
from goplay.sinaweibo.models import UserInfo
from django.shortcuts import render_to_response
from django.template import RequestContext

def tags_search(request,tag):
    results=[]
    tag=tag.encode('utf8')
    count=Activity.objects.filter(activityname=tag).count()
    if count<=10:
        activitys=Activity.objects.filter(activityname=tag).values('id','username','activityname','date','member','datetime')
    else:
        activitys=Activity.objects.filter(activityname=tag).values('id','username','activityname','date','member','datetime')[0:9]
    if activitys:
        for activity in activitys:
            image=UserInfo.objects.filter(username=activity.get("username")).values('id','profile_image')
            img=image[0]
            img.update(userid=img.pop('id'))#更改键值id为userid,防止与活动的id重名冲突
            results.append(dict(activity,**img))
        return render_to_response("activity/search.html",{"activitys":results},context_instance=RequestContext(request))
    else:
        return render_to_response("activity/search.html",{"activitys":"null"},context_instance=RequestContext(request))